# coding=utf-8

import time
from selenium import webdriver
from lxml import etree

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

friend = '254084330'
user = "286093858"
pw = "19940601glys"

next_num = 0

driver = webdriver.Firefox()
driver.maximize_window()
driver.get("http://i.qq.com")
driver.switch_to.frame("login_frame")
driver.find_element_by_id("switcher_plogin").click()
driver.find_element_by_id("u").send_keys(user)
driver.find_element_by_id("p").send_keys(pw)
driver.find_element_by_id("login_button").click()
# 让webdriver操纵当前页
driver.switch_to.default_content()
# 朋友的说说页面页面
driver.get("http://user.qzone.qq.com/" + friend + "/311")


while True:
    for i in range(1,6):
        height = 20000
        str_word = "window.scrollBy(0,"+str(height)+")"
        driver.execute_script(str_word)
        time.sleep(4)
        # 很多时候网页由多个<frame>或<iframe>组成，
        # webdriver默认定位的是最外层的frame，        
        # 所以这里需要选中一下说说所在的frame，否则找不到下面需要的网页元素

    driver.switch_to.frame("app_canvas_frame")
    selector = etree.HTML(driver.page_source)
    divs = selector.xpath('//*[@id="msgList"]/li/div[3]')

    #这里使用 a 表示内容可以连续不清空写入

    with open("qq_word_kk.txt",'a') as f:
        for div in divs:
            qq_name = div.xpath("./div[2]/a/text()")
            qq_content = div.xpath("./div[2]/pre/text()")
            qq_time = div.xpath("./div[4]/div[1]/span/a/text()")

            qq_name = qq_name[0] if len(qq_name) > 0 else ''
            qq_content = qq_content[0] if len(qq_content)>0 else ''
            qq_time = qq_time[0] if len(qq_time) > 0 else ''

            print qq_name, qq_time, qq_content
            f.write(qq_content + "\n")

    if driver.page_source.find('pager_next_' + str(next_num)) == -1:
        break
    driver.find_element_by_id('pager_next_' + str(next_num)).click()
    next_num += 1
    driver.switch_to.parent_frame()















